#include <REGX52.H>
#include <intrins.h>
#include "car.h"

sbit Trig=P1^1;
sbit Echo=P1^2;

unsigned int sum;
unsigned int mindistance=20;

void Delay1000ms()		//@11.0592MHz
{
  unsigned char i, j, k;

  _nop_();
  i = 8;
  j = 1;
  k = 243;
  do
    {
      do
        {
          while (--k);
        }
      while (--j);
    }
  while (--i);
}



void Delay20us()		//@11.0592MHz
{
  unsigned char i;

  _nop_();
  i = 6;
  while (--i);
}

void chaoshengbo()
{
  TMOD=0x11;
  TL1=0;
  TH1=0;

  Trig=1;
  Delay20us();
  Trig=0;
  while(!Echo);
  TR1=1;
  while(Echo);
  TR1=0;

  sum = ((TH1*256+TL1)*0.034)/2+1;

  if(mindistance>sum)
    {
      car_stop();
      Delay1000ms();
    }
}